using System;
using System.Globalization;

namespace ICalendar.CalendarProperties
{
    public abstract class TextBase : CalendarPropertyBase<string>
    {
        protected TextBase(string description) : this(description,null,null)
        {

        }

        protected TextBase(string description, CultureInfo language, Uri alternateTextRepresentation)
        {
            this.Value = description;
            this.Language = language;
            this.AlternateTextRepresentation = alternateTextRepresentation;
        }

        /// <summary>
        /// To specify an alternate text representation for the property value.
        /// </summary>
        public Uri AlternateTextRepresentation { get; set; }

        /// <summary>
        /// If the LANGUAGE property parameter is specified, the identified language applies to the CN parameter.
        /// </summary>
        public CultureInfo Language { get; set; }

        protected override void SetValue(string value)
        {
            this.Value = value;
        }

        protected override string GetValue()
        {
            return this.Value;
        }

        protected override void SetParametersFromXParameters(XParameterCollection xparams)
        {
            if (xparams == null)
                return;

            this.Value = GetValue();
            var tempDIR = xparams.GetValue(ReservedNames.ParameterAlternateTextRepresentation);
            if (tempDIR != null)
            {
                this.AlternateTextRepresentation = new Uri(tempDIR.Replace("\"", ""));
            }

            var language = xparams.GetValue(ReservedNames.ParameterLanguage);
            if (language != null)
            {
                this.Language = CultureInfo.GetCultureInfo(language);
            }
        }

        protected override XParameterCollection GetXParametersFromParameters()
        {
            return new XParameterCollection
                       {
                           {ReservedNames.ParameterAlternateTextRepresentation, string.Format("\"{0}\"", this.AlternateTextRepresentation)},
                           {ReservedNames.ParameterLanguage, Language != null ? Language.Name : null}
                       };
        }

        protected override string[] GetXReservedParametersNames()
        {
            return new[] {ReservedNames.ParameterAlternateTextRepresentation, ReservedNames.ParameterLanguage};
        }
    }
}